﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using LanceCarol.Utility;

namespace CSI300Cons.CapitalDownloader
{
    public class PriceRecord
    {
        #region DataMembers

        public int StockID { get; set; }
        public string StockName { get; set; }

        /// <summary>
        /// 当前价格(直接下载)
        /// </summary>
        public double CurrentPrice { get; set; }

        /// <summary>
        /// 总股本(直接下载)
        /// </summary>
        public double TotalCapital { get; set; }

        /// <summary>
        /// 总市值(直接下载)
        /// </summary>
        public double TotalValue { get; set; }

        /// <summary>
        /// 流通股本(直接下载)
        /// </summary>
        public double CurrentCapital { get; set; }

        public DateTime DownloadDate { get; set; }

        #endregion

        #region Properites

        /// <summary>
        /// 流通股本/总股本=BL
        /// </summary>
        public double BL
        {
            get
            {
                return this.CurrentCapital / this.TotalCapital;
            }
        }

        /// <summary>
        /// 靠档后XBL：BL<10%:  不变
        ///   BL<20%,<30%,<40%,<50%,<60%,<70%,<80%:
        /// 分别设为20%，30%，40%，50%，60%，70%，80%
        ///  BL>80%: 100%
        /// </summary>
        private double xbl = -1.0;
        public double XBL
        {
            get
            {
                if (xbl < 0)
                {
                    int t = (int)(BL * 100) / 10;

                    switch (t)
                    {
                        case 0:
                            xbl= BL;
                            break;
                        case 8:
                        case 9:
                        case 10:
                            xbl = 1.0;
                            break;
                        default:
                            xbl = (t + 1) * 0.1;
                            break;
                    }
                }
                return xbl;
            }
        }

        private double adjustedCapital = -1.0;

        /// <summary>
        /// 调整股本 = XBL*总股本
        /// </summary>
        public double AdjustedCapital
        {
            get
            {
                if (adjustedCapital<0)
                {
                    adjustedCapital=this.XBL * this.TotalCapital; 
                }
                return adjustedCapital;
            }
        }

        #endregion

        #region Methods

        private const string CMD_Save = "INSERT INTO PriceRecord([StockID],[StockName],[CurrentPrice],[TotalCapital],[TotalValue],[DownloadDate]) VALUES (@StockID,@StockName,@CurrentPrice,@TotalCapital,@TotalValue,@DownloadDate)";
        private const string CMD_ClearToday = "DELETE FROM PriceRecord WHERE [DownloadDate] = @DownloadDate";

        public void Save()
        {
            OleDbParameter[] cmdParms = GetSaveParameters();
            cmdParms[0].Value = this.StockID;
            cmdParms[1].Value = this.StockName;
            cmdParms[2].Value = this.CurrentPrice;
            cmdParms[3].Value = this.TotalCapital;
            cmdParms[4].Value = this.TotalValue;
            cmdParms[5].Value = this.DownloadDate;
            DbHelperOleDb.ExecuteSql(CMD_Save, cmdParms);
        }

        private static OleDbParameter[] GetSaveParameters()
        {
            OleDbParameter[] parms = DbHelperOleDb.GetCachedParameters(CMD_Save);

            if (parms == null)
            {
                parms = new OleDbParameter[] {
                new OleDbParameter("@StockID", OleDbType.Integer), 
                new OleDbParameter("@StockName", OleDbType.VarChar, 0xff), 
                new OleDbParameter("@CurrentPrice", OleDbType.Decimal), 
                new OleDbParameter("@TotalCapital", OleDbType.Decimal), 
                new OleDbParameter("@TotalValue", OleDbType.Decimal), 
                new OleDbParameter("@DownloadDate", OleDbType.Date) };

                DbHelperOleDb.CacheParameters(CMD_Save, parms);
            }
            return parms;
        }

        public static void ClearToday()
        {
            OleDbParameter[] cmdParms = GetClearTodayParameters();
            cmdParms[0].Value = DateTime.Today;
            DbHelperOleDb.ExecuteSql(CMD_ClearToday, cmdParms);
        }

        private static OleDbParameter[] GetClearTodayParameters()
        {
            OleDbParameter[] parms = DbHelperOleDb.GetCachedParameters(CMD_ClearToday);

            if (parms == null)
            {
                parms = new OleDbParameter[] {
                new OleDbParameter("@DownloadDate", OleDbType.Date) };

                DbHelperOleDb.CacheParameters(CMD_ClearToday, parms);
            }
            return parms;
        }

        #endregion

    }
}
